import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;


public class VendingMachineTest {
	VendingMachine vnd1;
	VendingMachineItem itm1;
	VendingMachineItem itm2;
	
	@Before
	public void setUp() throws Exception {
		vnd1 = new VendingMachine();
		itm1 = new VendingMachineItem("Coke", 1.50);
		itm2 = new VendingMachineItem("Diet Coke", 0.00);
	}
	
	@After
	public void TearDown() throws Exception {
		vnd1 = null;
		itm1 = null;
	}

	@Test
	public void testAddItem_OneItem_A() {
				
		vnd1.addItem(itm1, "A");
		assertEquals(itm1, vnd1.getItem("A"));
	}
	
	@Test
	public void testAddItem_OneItem_B() {
				
		vnd1.addItem(itm1, "B");
		assertEquals(itm1, vnd1.getItem("B"));
	}

	@Test
	public void testAddItem_OneItem_C() {
				
		vnd1.addItem(itm1, "C");
		assertEquals(itm1, vnd1.getItem("C"));
	}

	@Test
	public void testAddItem_OneItem_D() {
				
		vnd1.addItem(itm1, "D");
		assertEquals(itm1, vnd1.getItem("D"));
	}
	@Test
	public void testAddItem_OneItem_InvalidCode() {
		try
		{
			vnd1.addItem(itm1, "E");
			assertEquals(itm1, vnd1.getItem("E"));
		}
		catch (VendingMachineException e)
		{
			
		}
		
	}

	@Test
	(expected = VendingMachineException.class)
	public void testAddItem_MultipleItem() {
				
		vnd1.addItem(itm1, "A");
		vnd1.addItem(itm1, "A");
		assertEquals(itm1, vnd1.getItem("A"));
		
		vnd1.addItem(itm2, "B");
		vnd1.addItem(itm2, "B");
		assertEquals(itm2, vnd1.getItem("B"));
		
	}
	
	@Test
	public void testRemoveItem() {
		vnd1.addItem(itm1, "A");
		
		vnd1.removeItem("A");
		assertEquals(null, vnd1.getItem("A") );
	}

	@Test
	(expected = VendingMachineException.class)
	public void testRemoveItemInvalidCode() {	
		vnd1.removeItem("C");
	}

	
	@Test
	public void testInsertMoney() {
		vnd1.insertMoney(1.00);
		assertEquals(1.00,vnd1.balance,0);
	}
	
	@Test(expected = VendingMachineException.class)
	public void testInsertMoneyNegativeBalance() {
		vnd1.insertMoney(-1.00);
		
	}

	@Test
	public void testGetBalance() {
		vnd1.insertMoney(1.00);
		assertEquals( 1.00, vnd1.getBalance(),0);
	}

	@Test
	public void testMakePurchaseNoChange() {
		vnd1.addItem(itm1, "A");
		
		vnd1.makePurchase("A");
		assertEquals(vnd1.balance, 0.00,0);
	}
	@Test
	public void testMakePurchaseWithChange()
	{
		VendingMachineItem itm3;
		itm3 = new VendingMachineItem("Diet Sprite", 2.00);
		
		vnd1.addItem(itm1, "A");
		vnd1.addItem(itm3, "C");
	
		vnd1.balance = 4.00;
		assertTrue(vnd1.makePurchase("A"));
	}
	@Test
	public void testReturnChange() {
		vnd1.addItem(itm1, "A");
			
		vnd1.makePurchase("A");
		vnd1.returnChange();
		assertEquals(0.00, vnd1.balance,0);
	}

}
